package com.sms;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.teaopenapi.models.Config;
import org.springframework.stereotype.Component;


public class SmsTemplate {

    private SmsProperties smsProperties;
    public SmsTemplate(SmsProperties smsProperties) {
        this.smsProperties = smsProperties;
    }
    public void send(String mobile,String code){
        try {
            Config config = new Config()
                    .setAccessKeyId(smsProperties.getAccessKey())
                    .setAccessKeySecret(smsProperties.getSecret());
            config.endpoint = "dysmsapi.aliyuncs.com";
            Client client = new Client(config);

            SendSmsRequest sendSmsRequest = new SendSmsRequest()
                    .setSignName(smsProperties.getSignName())
                    .setTemplateCode(smsProperties.getTemplateCode())
                    .setPhoneNumbers(mobile)
                    .setTemplateParam("{\"code\":\""+code+"\"}");
            client.sendSms(sendSmsRequest);
            System.out.println("ok");


        } catch (Exception e) {
            e.printStackTrace();
        }

    }


}
